package info.niwota.start.app;

import info.niwota.start.Debug;
import info.niwota.start.R;
import info.niwota.start.ResourceInfo;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;

public class AppListActivity extends ListActivity implements
		android.view.View.OnClickListener {

	private static final String TAG = "AppListActivity";

	@Override
	protected void onPause() {
		super.onPause();
		if (Debug.DEBUG) {
			Log.d(TAG, "onPause");
		}
	}


	@Override
	protected void onResume() {
		super.onResume();
		if (Debug.DEBUG) {
			Log.d(TAG, "onResume");
		}
	}


	protected ArrayList<ResourceInfo> apps = new ArrayList<ResourceInfo>();

	private AppListAdapter adapter;


	private void updateUi() {
		if (Debug.DEBUG) {
			Log.d(TAG, "updateUi");
		}
		getListView().setAdapter(adapter);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onCreate: " + savedInstanceState);
		}
		
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		
		setContentView(R.layout.app_list);
	
		adapter = new AppListAdapter(this, apps);
		//
		final ListView lv = getListView();
		lv.setAdapter(adapter);
		lv.setItemsCanFocus(true);
		lv.setChoiceMode(ListView.CHOICE_MODE_NONE);
		//
		registerForContextMenu(lv);
		
		getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon);

		if (savedInstanceState != null) {
			updateUi();
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onActivityResult requestCode: " + requestCode
					+ " resultCode: " + resultCode + " data: " + data);
		}
	}

	
	@Override
	protected void onStart() {
		super.onStart();
		
		if (Debug.DEBUG) {
			Log.d(TAG, "onStart");
		}
		
		loadApps();
		
		getListView().setAdapter(adapter);
	}

	protected void loadApps() {
	}
	
	public void onClick(View v) {
		//
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return super.onOptionsItemSelected(item);
	}
	
	protected void doOpen(ResourceInfo item) {
		final Context ctx = this;
		try {
			Intent i = item.getIntent();
			ctx.startActivity(i);
		} catch (Exception e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT);
		}
	}

	@Override
	protected void onListItemClick(ListView list, View view, int pos, long id) {
		ResourceInfo ri = adapter.getItem(pos);
		
		if (Debug.DEBUG) {
			Log.d(TAG, "onListItemClick " + ri);
		}
		
		doOpen(ri);
		//
		finish();
	}
}
